package com.fyl.utils;

import com.alibaba.fastjson.JSONObject;
import com.fyl.core.advance.constant.IResponseEnum;
import com.fyl.core.response.RS;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResponseUtil {


    public static void cutResponse(int code, String message, HttpServletResponse response) throws IOException {
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpStatus.OK.value());
        PrintWriter writer = response.getWriter();
        writer.print(JSONObject.toJSON(RS.genFail(code, message)));
        writer.close();
        response.flushBuffer();
    }

    public static <T extends IResponseEnum> void cutResponse(T respEnum, HttpServletResponse response) throws IOException {
        cutResponse(respEnum.getCode(), respEnum.getMessage(), response);
    }
}

